package com.titar.robot.http;

import android.content.Context;
import android.util.Log;

import com.titar.robot.Constants;
import com.titar.robot.RobotApp;
import com.titar.robot.http.interceptor.BasicAuthInterceptor;
import com.titar.robot.http.interceptor.SSL;
import com.titar.robot.http.service.CustomerService;
import com.titar.robot.utils.LiveDataCallAdapterFactory;


import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.titar.robot.Constants.PORT;

public class RetrofitApi {
    private static Retrofit retrofit;
    private static final int DEFAULT_TIMEOUT = 10;//超时时间
    private static volatile CustomerService customerService = null;

    /**
     * 创建网络请求接口实例
     *
     * @return
     */
    public static CustomerService getRequestInterface() {
        if (customerService == null) {
            synchronized (CustomerService.class) {
                customerService = provideRetrofit(RobotApp.getContext()).create(CustomerService.class);
            }
        }
        return customerService;
    }

    /**
     * 初始化必要对象和参数
     *
     * @return
     */
    private static Retrofit provideRetrofit(Context context) {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .addInterceptor(getHttpLoggingInterceptor())//Application拦截器
                .addNetworkInterceptor(getRequestHeader(context))//Network拦截器
                .sslSocketFactory(SSL.getSSLC().getSocketFactory(), SSL.UnSafeTrustManager)
                .hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                })
                .build();
        retrofit = new Retrofit.Builder()
                .baseUrl(Constants.HOST + ":" + PORT)//设置网络请求的Url地址
                .addConverterFactory(GsonConverterFactory.create())//设置数据解析器
                .addCallAdapterFactory(new LiveDataCallAdapterFactory())//支持RxJava平台
                .client(client)
                .build();
        return retrofit;
    }

    /**
     * 日志拦截器
     *
     * @return
     */
    private static HttpLoggingInterceptor getHttpLoggingInterceptor() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.e("OkHttpLog", "log = " + message);
            }
        });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        return loggingInterceptor;
    }

    /**
     * 请求头拦截器
     *
     * @return
     */
    private static Interceptor getRequestHeader(Context context) {
        Interceptor headerInterceptor = new BasicAuthInterceptor(context);
        return headerInterceptor;
    }
}
